home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / www / RaiskaaHTML.lha / raiskaahtml / RaiskaaHTML.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-09-24  |  8.5 KB  |  304 lines

  1. /*
  2.  * RaiskaaHTML.rexx -- Usable frontend to TodellakinRaiskaaHTML.
  3.  *
  4.  * RaiskaaHTML strips all sorts of junk from HTML documents. As it only
  5.  * works on pipes and has a Unix-compatible bullshit syntax, this ARexx
  6.  * script provides a more reasonable way of using it.
  7.  *
  8.  * Updates are available from aminet:comm/www/RaiskaaHTML.lha
  9.  *
  10.  * Copyright © 1998 by Thomas Aglassinger <agi@sbox.tu-graz.ac.at>
  11.  */
  12. /* $VER: RaiskaaHTML.rexx 1.0 (24.9.98) */
  13. release_version = '1.0'
  14. release_date = '24.9.98'
  15.  
  16. /* Initialization */
  17. Options Results
  18. Parse Arg arguments
  19. Failat 6
  20.  
  21. /* Add libraries. No error checking because the AddLib() return code sucks */
  22. Call AddLib('rexxsupport.library', 0, -30, 0)
  23. Call AddLib('rexxdossupport.library', 0, -30, 2)
  24.  
  25. /* Support filenames */
  26. temporary_filename = 't:RaiskaaHTML-' || Pragma('ID') || '.temporary'
  27. error_filename     = 't:RaiskaaHTML-' || Pragma('ID') || '.error'
  28. list_filename      = 't:RaiskaaHTML-' || Pragma('ID') || '.list'
  29.  
  30. return_code = 0
  31.  
  32. /* Initilize arguments */
  33. template_1 = 'From/A,To/K'
  34. template_2 = 'Blink/S,Color/S,DocType/S,Font/S,Heading/S,Link/S,Meta/S,Quiet/S,Ruler/S,ScMsg/S,Script/S,SGML/S,Space/S,Table/S,Toggle/S'
  35. template = template_1 || ',' || template_2
  36. from = ''
  37. to = ''
  38.  
  39. Blink = 0
  40. Color = 0
  41. DocType = 0
  42. Font = 0
  43. SGML = 0
  44. Heading = 0
  45. Link = 0
  46. Meta = 0
  47. Space = 0
  48. Ruler = 0
  49. Script = 0
  50. Table = 0
  51.  
  52. /* Previous length of status messages */
  53. status_length = 0
  54.  
  55. /* Constants */
  56. error_in_arguments = 'error in arguments: '
  57.  
  58. /* Parse arguments */
  59. if ~ReadArgs(arguments,template) then do
  60.    Say error_in_arguments || Fault(RC)
  61.    Exit 10
  62. end
  63.  
  64. /* Toggle options if requested */
  65. if Toggle then do
  66.    Blink = ~Blink
  67.    Color = ~Color
  68.    DocType = ~DocType
  69.    Font = ~Font
  70.    SGML = ~SGML
  71.    Heading = ~Heading
  72.    Link = ~Link
  73.    Meta = ~Meta
  74.    Space = ~Space
  75.    Ruler = ~Ruler
  76.    Script = ~Script
  77.    Table = ~Table
  78. end
  79.  
  80. /* Compute arguments for RaiskaaHTML */
  81. raiskaa_options = ''
  82. if ~Blink   then raiskaa_options = raiskaa_options || 'b'
  83. if ~Color   then raiskaa_options = raiskaa_options || 'c'
  84. if ~DocType then raiskaa_options = raiskaa_options || 'd'
  85. if ~Font    then raiskaa_options = raiskaa_options || 'f'
  86. if ~SGML    then raiskaa_options = raiskaa_options || 'g'
  87. if ~Heading then raiskaa_options = raiskaa_options || 'h'
  88. if ~Link    then raiskaa_options = raiskaa_options || 'l'
  89. if ~Meta    then raiskaa_options = raiskaa_options || 'm'
  90. if ~Space   then raiskaa_options = raiskaa_options || 'o'
  91. if ~Ruler   then raiskaa_options = raiskaa_options || 'r'
  92. if ~Script  then raiskaa_options = raiskaa_options || 's'
  93. if ~Table   then raiskaa_options = raiskaa_options || 't'
  94.  
  95. source_filename = From
  96. target_filename = to
  97.  
  98. /* Convert possible URI to filename */
  99. source_filename = strip_from_begining(source_filename, 'file://localhost/')
  100. source_filename = strip_from_begining(source_filename, 'file:///')
  101.  
  102. /* Check, if FROM argument denotes a directory */
  103. source_type = Word(StateF(source_filename), 1)
  104.  
  105. if (source_type = 'FILE') then do
  106.    /* Process a single file */
  107.    Call raiskaa_file(source_filename, target_filename)
  108. end
  109. else do
  110.    /* Process all files in a directory */
  111.    if target_filename = '' then do
  112.       Call status_message('scanning "' || source_filename || '" for documents')
  113.       list_command = 'list >"' || list_filename || '" "' || source_filename || '" all pat=#?.(html|shtml|htm) lformat=%p%n'
  114.       Address command list_command
  115.       Call clear_status_message
  116.  
  117.       if Open('list_file', list_filename, 'read') then do
  118.          do while ~Eof('list_file')
  119.             Line = ReadLn('list_file')
  120.             if (Line ~= '') then do
  121.                Call raiskaa_file(line, '')
  122.             end
  123.          end
  124.  
  125.          Call Close('list_file')
  126.          Delete(list_filename)
  127.       end
  128.    end
  129.    else do
  130.       Say error_in_arguments || 'option TO not allowed with FROM directory'
  131.       return_code = 10
  132.    end
  133. end
  134.  
  135. /* Cleanup */
  136. Call Delete(temporary_filename)
  137. Delete(error_filename)
  138.  
  139. exit return_code
  140.  
  141. /* Format file size */
  142. format_size: Procedure
  143.    Parse Arg size
  144.  
  145.    if Length(size) < 6 then do
  146.       size = Right(size,6)
  147.    end
  148.  
  149.    return size
  150.  
  151. /* Send message to ScMsg message browser */
  152. send_scmsg:
  153.    Parse Arg filename, line, class, number, text
  154.    filename = AbsolutePath(filename)
  155.    scmsg_command = 'NewMsg "' || filename || '" "' || filename ||'" ' || Strip(line,'B') || ' 0 "" 0 ' || Strip(class,'B') Strip(number,'B') Strip(text,'L')
  156.    Address  'SC_SCMSG' scmsg_command
  157.    Return
  158.  
  159. /* Clear status message */
  160. clear_status_message:
  161.    if ~Quiet then do
  162.       WriteCh('STDOUT', Copies(' ', status_length+1) || d2c(13))
  163.    end
  164.    return
  165.  
  166. /* Show status message */
  167. status_message:
  168.    Parse Arg message
  169.    if ~Quiet then do
  170.       Call clear_status_message
  171.       WriteCh('STDOUT', ' ' || message || d2c(13))
  172.       status_length = Length(message)
  173.    end
  174.    return
  175.  
  176. /* Strip string from begining if input starts with it */
  177. strip_from_begining: procedure
  178.    Parse Arg source_text, strip_text
  179.  
  180.    strip_length = Length(strip_text)
  181.    if Left(source_text, strip_length) = strip_text then do
  182.       source_text = DelStr(source_text, 1, strip_length)
  183.    end
  184.  
  185.    return source_text
  186.  
  187. /* Raiskaa one file */
  188. raiskaa_file:
  189.    Parse Arg source_filename, target_filename
  190.  
  191.    /* If no target specified, overwrite source */
  192.    old_target_filename = target_filename;
  193.    if target_filename = '' then do
  194.       target_filename = source_filename;
  195.    end
  196.  
  197.    /* Check for SAS/c message browser running; launch it if not */
  198.    if ScMsg then do
  199.       /* Invoke ScMsg, if it's not already there */
  200.       if ~Show('ports', 'SC_SCMSG') then do
  201.          Call status_message('invoking ScMsg')
  202.          Address Command 'run <>nil: sc:c/scmsg HIDDEN'
  203.          Address Command 'WaitForPort SC_SCMSG'
  204.       end
  205.  
  206.       /* Check, if ScMsg showed up */
  207.       if ~Show('ports', 'SC_SCMSG') then do
  208.          Say "warning: could not start ScMsg, using console"
  209.          ScMsg = 0
  210.       end
  211.       else do
  212.          /* Remove old messages in ScMsg-window */
  213.          Address 'SC_SCMSG' 'DelComp "' || AbsolutePath(source_filename) || '"'
  214.       end
  215.    end
  216.  
  217.    /* Remember size of source file */
  218.    Call status_message(source_filename)
  219.    if ~Quiet then do
  220.       old_size = StateF(source_filename)
  221.       Parse Var old_size . ' ' old_size .
  222.    end
  223.  
  224.    /* Invoke TodellakinRaiskaaHTML */
  225.    command_raiskaa = 'TodellakinRaiskaaHTML "' || raiskaa_options || '" "' || error_filename || '" <"' || source_filename || '" >' || temporary_filename
  226.    Address Command command_raiskaa
  227.    raiskaa_result = RC
  228.  
  229.    /* Handle errors */
  230.    if raiskaa_result > 0 then do
  231.  
  232.       /* Update return code */
  233.       if (raiskaa_result = 1) & (return_code < 5) then do
  234.          return_code = 5
  235.       end
  236.       else if (raiskaa_result = 2) & (return_code < 10) then do
  237.          return_code = 10
  238.       end
  239.       else do
  240.          return_code = 20
  241.       end
  242.  
  243.       /* Read error file and show it in ScMsg or console */
  244.       if Open('error_file', error_filename, 'read') then do
  245.  
  246.          message_counter = 0
  247.  
  248.          do while ~Eof('error_file')
  249.             message = ReadLn('error_file')
  250.             if (message ~= '') then do
  251.                message_counter = message_counter + 1
  252.  
  253.                /* Show message in console or browser */
  254.                if ScMsg then do
  255.                   Parse Var message line ',' column ': ' class ':' text
  256.                   class = Upper(left(class,1)) || SubStr(class,2)
  257.                   Call send_scmsg(source_filename, line, class, 0, text)
  258.                end
  259.                else do
  260.                   WriteLn('STDOUT', source_filename || ':' || message)
  261.                end
  262.             end
  263.          end
  264.  
  265.          /* Pop up message browser */
  266.          if ScMsg & (message_counter > 0) then do
  267.             Address 'SC_SCMSG' 'Show'
  268.          end
  269.  
  270.          Call Close('error_file')
  271.       end
  272.    end
  273.  
  274.    if raiskaa_result < 2 then do
  275.       /* Move temporary file to target */
  276.       command_copy = 'copy quiet clone ' || temporary_filename || ' to "' || target_filename || '"'
  277.       Address Command command_copy
  278.  
  279.       /* View raiskas report */
  280.       if ~Quiet then do
  281.          new_size = StateF(target_filename)
  282.          Parse Var new_size . ' ' new_size .
  283.  
  284.          percent = 100
  285.          if new_size > 0 then do
  286.             percent = Trunc(100 - (new_size / old_size * 100), 1)
  287.          end
  288.  
  289.          if Length(percent) > 3 then do
  290.             percent = Trunc(percent,0)
  291.          end
  292.  
  293.          Call clear_status_message
  294.          Say 'Raiskattu: (' || Right(percent,3) || '%) ' || format_size(old_size) || ' => ' || format_size(new_size) || ': ' || source_filename
  295.       end
  296.    end
  297.  
  298.    /* Restore target filename */
  299.    target_filename = old_target_filename;
  300.  
  301.    return raiskaa_result
  302.  
  303.  
  304.